﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Text;

using Fuse8.DomainFramework.BusinessService;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Special;
using Fuse8.ContentManagementFramework.BizFacade.Services;
using Telerik.Web.Mvc;

namespace Fuse8.ContentManagementFramework.Administration.Controllers
{
    public abstract class CmsComplexEntityController<T> : CmsAdminController<T>
        where T : CmsComplexEntityObject, new()
    {
        protected virtual IEntityBusinessService<T, int> EntityService 
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        public virtual ActionResult EntityProperty(int id)
        {
            ActionResult result = null;

            var entity = EntityService.FindOne(id);
            if (entity != null)
            {
                var data = EntityMetaInformationService.GetMetaForEntity(entity) ?? new CmsEntityMetaInformation();

                data.Id = id;

                result = PartialView(data);
            }

            return result ?? new EmptyResult();
        }

        [GridAction]
        public ActionResult _EntityProperty(int id)
        {
            IList<CmsEntityMeta> data = null;

            var entity = EntityService.FindOne(id);
            if (entity != null)
            {
                var dataContainer = EntityMetaInformationService.GetMetaForEntity(entity) ?? new CmsEntityMetaInformation();
                
                dataContainer.Id = id;
                data = dataContainer.Metas;
            }

            return View(
                   new GridModel<CmsEntityMeta>
                   {
                       Data = data ?? new List<CmsEntityMeta>()
                   });
        }

        [HttpPost]
        public virtual ActionResult AddProperty(int id, string propertyName, string propertyValue)
        {
            return AddOrEditProperty(id, propertyName, propertyValue);
        }

        [HttpPost]
        public virtual ActionResult EditProperty(int id, string propertyName, string propertyValue)
        {
            return AddOrEditProperty(id, propertyName, propertyValue);
        }

        private ActionResult AddOrEditProperty(int id, string propertyName, string propertyValue)
        {
            ActionResult result = null;

            if (!string.IsNullOrWhiteSpace(propertyName) && !string.IsNullOrWhiteSpace(propertyValue))
            {
                var entity = EntityService.FindOne(id);
                if (entity != null)
                {
                    try
                    {
                        _AddOrEditProperty(entity, propertyName, propertyValue);

                        result = Json(1);
                    }
                    catch (Exception ex)
                    {
                        ControllerContext.HttpContext.Response.StatusCode = 500;
                        result = Content(ex.Message, "text/html", Encoding.UTF8);
                    }
                }
            }

            return result ?? Json(0);
        }

        protected virtual void _AddOrEditProperty(T entity, string propertyName, string propertyValue)
        {
            entity.SetMetaValue<string>(propertyName, propertyValue);

            EntityService.Save(entity);
        }
    }
}
